package znbkListRdqh.fragment;

import android.os.Bundle;
import android.text.Html;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.lancooznbk.R;

import znbkListRdqh.isNeedPaperAssembly.ILoadDateViewAssembly;
import znbkListRdqh.entity.WeekAscensionListEntity;
import znbkListRdqh.paperAssembly.WeekAscensionMakePaperPresenter;
import znbkListRdqh.presenter.WeekAscensionListPresenter;

import com.example.lancooznbk.view.adapter.PopupMenuType1Adapter;
import znbkListRdqh.adapter.WeekAscensionListAdapter;
import znbkListRdqh.isNeedPaperAssembly.NeedPaperAssemblyPresenter;

import com.example.lancooznbk.view.base.BaseMainFragment;
import com.example.lancooznbk.view.base.ILoadMoreDataView;
import com.example.lancooznbk.view.widget.CustomFooterView;
import com.example.lancooznbk.view.widget.PopupMenu;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2018\2\2 0002.
 */

public class WeekAscensionListFragment extends BaseMainFragment<WeekAscensionListPresenter> implements View.OnClickListener, ILoadMoreDataView<WeekAscensionListEntity> ,ILoadDateViewAssembly {



    private WeekAscensionListAdapter mWeekAscensionListAdapter;

    private TextView mTvSpStatus;
    private TextView mTvTotal;

    private PopupMenuType1Adapter popupMenuSubjectAdapter;
    private PopupMenu subjectPopupMenu;
    private View masker;
    private RelativeLayout mSpLayout;
    private Map<String,Integer> mQuesStatusMap = new HashMap<>();


    /*
    * 是否需要组卷
    * */
    private NeedPaperAssemblyPresenter mNeedPaperAssemblyPresenter;
    private void IsNeedPaperAssembly(){
        mNeedPaperAssemblyPresenter = new NeedPaperAssemblyPresenter(this,2);
        mNeedPaperAssemblyPresenter.getData();
    }


    /*
    * 开始组卷
    * */
    private WeekAscensionMakePaperPresenter mWeekAscensionMakePaperPresenter;
    private void WeekAscensionMakePaper(){
        mWeekAscensionMakePaperPresenter = new WeekAscensionMakePaperPresenter(this);
        mWeekAscensionMakePaperPresenter.getData();
    }


    @Override
    protected void updateViews(boolean refresh) {
        mPresenter.getData(refresh);
    }

    @Override
    public int attachLayoutRes() {
        return R.layout.fragment_list_week_ascension;
    }

    @Override
    public void initBaseMainViews(View view, Bundle savedInstanceState) {
        mPresenter = new WeekAscensionListPresenter(this);

        mSpLayout = (RelativeLayout) view.findViewById(R.id.rel_toolbar_head);
        mTvSpStatus = (TextView) view.findViewById(R.id.sp_status);
        mTvTotal = (TextView) view.findViewById(R.id.tv_paperNumber);
        masker = view.findViewById(R.id.bg_masker);
        View toolBarLayout = view.findViewById(R.id.rel_toolbar_layout);
        ((TextView)toolBarLayout.findViewById(R.id.tv_toolbar_title)).setText(R.string.menu_rdqh);
        ((LinearLayout)toolBarLayout.findViewById(R.id.layout_toolbar_back)).setOnClickListener(this);
//        toolBarLayout.findViewById(R.id.img_main_menu).setOnClickListener(this);

        ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) toolBarLayout.getLayoutParams();
        layoutParams.setMargins(0,getStatusBarHeight(),0,0);
        // mToolbar = (Toolbar) view.findViewById(R.id.toolbar);
        //initToolBar(mToolbar,false,R.string.baseReview);
        mTvSpStatus.setOnClickListener(this);
        String[] quesStatusArrays = getResources().getStringArray(R.array.QuesStatusArraysXtfxRdqh);
        String[] quesStatusNameArrays = new String[quesStatusArrays.length];
        for (int i = 0 ; i < quesStatusArrays.length ; i++){
            String[] split = quesStatusArrays[i].split("\\|");
            quesStatusNameArrays[i]  = split[0];
            mQuesStatusMap.put(split[0], Integer.valueOf(split[1]));
        }
        popupMenuSubjectAdapter = new PopupMenuType1Adapter(getHolderActivity(),quesStatusNameArrays);
        subjectPopupMenu = new PopupMenu(getHolderActivity(), LinearLayout.LayoutParams.MATCH_PARENT,R.drawable.bg_popupmenu);
        subjectPopupMenu.setListAdapter(popupMenuSubjectAdapter);
        subjectPopupMenu.setOnItemClickListener(new PopupMenu.OnItemClickListener() {
            @Override
            public void onItemClick(View item, int position, long id) {
                mTvSpStatus.setText(subjectPopupMenu.getCheckedStr());
                mXRefreshView.startRefresh();
                mPresenter.getDataByStatus(mQuesStatusMap.get(subjectPopupMenu.getCheckedStr()));
            }
        });
        mTvSpStatus.setText(subjectPopupMenu.getCheckedStr());
        subjectPopupMenu.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                masker.setVisibility(View.GONE);
                mTvSpStatus.setSelected(false);
            }
        });


        mWeekAscensionListAdapter = new WeekAscensionListAdapter(getContext());
        mWeekAscensionListAdapter.setCustomLoadMoreView(new CustomFooterView(getActivity()));
        mRecyclerView.setAdapter(mWeekAscensionListAdapter);
        mWeekAscensionListAdapter.setOnItemClickListener(new WeekAscensionListAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, List<WeekAscensionListEntity.ModelPaperInfoListBean> list, int position) {
                //ToastUtils.showShort("第"+(position+1)+"个");
                mPresenter.openItemDetail(position,list.get(position));
            }
        });

    }


    @Override
    public void showNetError() {
        super.showNetError();
        setPaperTotal(0);

    }


    @Override
    public void showNoData(String msg) {
        super.showNoData(msg);
        setPaperTotal(0);

    }

    @Override
    public void hideLoading() {
        super.hideLoading();

    }

    @Override
    public void loadData(WeekAscensionListEntity data) {
        if(mRecyclerView.getLayoutManager() != null ) {
            mRecyclerView.getViewTreeObserver().addOnGlobalLayoutListener(layoutListener);
        }

        setPaperTotal(data.getTotalCount());
        mWeekAscensionListAdapter.clearAndAdd(data.getModelPaperInfoList());
        loadComplete(mWeekAscensionListAdapter.getAdapterItemCount() >= data.getTotalCount());

    }

    @Override
    public void loadMoreData(WeekAscensionListEntity data) {
        setPaperTotal(data.getTotalCount());
        mWeekAscensionListAdapter.add(data.getModelPaperInfoList());
        loadComplete(mWeekAscensionListAdapter.getAdapterItemCount() >= data.getTotalCount());
    }





    /**
     * 没有试卷或者加载失败的逻辑处理
     */
    private void setPaperTotal(int number) {
        String paperTotal = String.format(getString(R.string.znbkPaperTotal),number);
        mTvTotal.setText(Html.fromHtml(paperTotal));
    }



    /*@Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu,inflater);
        inflater.inflate(R.menu.menu_main,menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == R.id.item_other_system){
            //ToastUtils.showShort("基本复习进度提示");

            return true;
        }
        return false;
    }*/

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.sp_status:
                masker.setVisibility(View.VISIBLE);//阴影布局可见
                mTvSpStatus.setSelected(true);
                subjectPopupMenu.showLocation(mSpLayout, 0, 0);
                break;
            case R.id.layout_toolbar_back:
                getHolderActivity().finish();
                break;
//            case R.id.img_main_menu:
//                startActivity(new Intent(getHolderActivity(), ReviewProcessActivity.class));
//                break;
        }


    }

    @Override
    public void onResume() {
        super.onResume();
        Log.e("","=========================onResume=====");
        IsNeedPaperAssembly();
    }

    @Override
    public void assemblyPaper() {
        Log.e("","=========================assemblyPaper=====");
        WeekAscensionMakePaper();
    }

    @Override
    public void assemblyPaperSuccessed() {
        Log.e("","=========================assemblyPaperSuccessed=====");
        updateViews(true);
    }
}